package category

import (
	"BookStack/internal/domain/category"
	"BookStack/pkg/utils"
)

// CreateCategoryRequest 创建分类请求
type CreateCategoryRequest struct {
	Name        string `json:"name" binding:"required,max=100"`
	Slug        string `json:"slug" binding:"required,max=100"` // 分类标识
	Type        int    `json:"type" binding:"required,oneof=1 2"`
	ParentID    uint   `json:"parent_id"`
	Description string `json:"description" binding:"max=500"`
	Order       int    `json:"order"`
	Status      int    `json:"status" binding:"oneof=0 1"`
}

// UpdateCategoryRequest 更新分类请求
type UpdateCategoryRequest struct {
	Name        string `json:"name" binding:"required,max=100"`
	Slug        string `json:"slug" binding:"required,max=100"` // 分类标识
	Type        int    `json:"type" binding:"required,oneof=1 2"`
	ParentID    uint   `json:"parent_id"`
	Description string `json:"description" binding:"max=500"`
	Order       int    `json:"order"`
	Status      int    `json:"status" binding:"oneof=0 1"`
}

// UpdateCategoryOrderRequest 更新分类排序请求
type UpdateCategoryOrderRequest struct {
	Order int `json:"order" binding:"required"`
}

// CategoryResponse 分类响应
type CategoryResponse struct {
	ID           uint           `json:"id"`
	Name         string         `json:"name"`
	Slug         string         `json:"slug"` // 分类标识
	Type         int            `json:"type"`
	ParentID     uint           `json:"parent_id"`
	Description  string         `json:"description"`
	Order        int            `json:"order"`
	Status       int            `json:"status"`
	ContentCount int64          `json:"content_count"`
	CreatedAt    utils.JSONTime `json:"created_at"`
	UpdatedAt    utils.JSONTime `json:"updated_at"`
}

// PageListQueryParams 分类列表查询参数
type CategoryListQueryParams struct {
	Page     int  `json:"page" form:"page"`
	PageSize int  `json:"page_size" form:"page_size" binding:"max=100"`
	Type     int  `json:"type" form:"type"`     // 分类类型：1=文章分类，2=书籍分类
	Status   *int `json:"status" form:"status"` // 分类状态：0=未发布，1=已发布，nil=查询所有状态
}

// FromEntity 从实体转换为DTO
func FromEntity(entity *category.Category) *CategoryResponse {
	if entity == nil {
		return nil
	}
	return &CategoryResponse{
		ID:           entity.ID,
		Name:         entity.Name,
		Slug:         entity.Slug,
		Type:         entity.Type,
		ParentID:     entity.ParentID,
		Description:  entity.Description,
		Order:        entity.Order,
		Status:       entity.Status,
		ContentCount: entity.ContentCount,
		CreatedAt:    utils.JSONTime(entity.CreatedAt),
		UpdatedAt:    utils.JSONTime(entity.UpdatedAt),
	}
}

// ToEntity 从DTO转换为实体
func (req *CreateCategoryRequest) ToEntity() *category.Category {
	return &category.Category{
		Name:        req.Name,
		Slug:        req.Slug,
		Type:        req.Type,
		ParentID:    req.ParentID,
		Description: req.Description,
		Order:       req.Order,
		Status:      req.Status,
	}
}
